<?php

if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Event extends AUTH_Controller {

  public function index() {
    redirect('event/create');
  }

  public function myevents() {
    $my_events = NULL;
    $get_my_events = $this->event_model->get_my_events($this->current_user['user_id'], 100);
    if ($get_my_events['status'] == 1 && !empty($get_my_events['result'])) {
      $my_events = $get_my_events['result'];
    }
    $view_data = array();
    $view_data['menu_active'] = 'events';
    $view_data['my_events'] = $my_events;
    $this->load_view('event_myevents', $view_data);
    return;
  }

  public function create() {
    $new_event_status = NULL;
    $event_date = NULL;
    $event_message = NULL;
    if ($this->is_post()) {
      $new_event_status = 3;
      $event_date = $this->sanitize($this->input->post('event_date', TRUE));
      $event_message = $this->sanitize($this->input->post('event_message', TRUE));
      if (!empty($event_date) && !empty($event_message)) {
        $new_event_status = 2;
        if (strtotime($event_date)) {
          $new_event_status = 3;
          if (strlen($event_message) < 251) {
            $set_event = $this->event_model->set_event($event_date, $event_message, $this->current_user['user_id']);
            if ($set_event['status'] == 1 && !empty($set_event['result'])) {
              $event_id = $set_event['result']['event_id'];
              $new_event_status = 1;
              $this->session->set_flashdata('new_event_status', $new_event_status);
              $this->session->set_flashdata('event_event_id', $event_id);
              $this->session->set_flashdata('event_event_hash', md5($event_id . 'event'));
              redirect('events');
              return;
            }
          }
        }
      }
      $this->session->set_flashdata('new_event_status', $new_event_status);
      redirect('event/create');
      return;
    }
    $my_events = NULL;
    $get_my_events = $this->event_model->get_my_events($this->current_user['user_id'], 10);
    if ($get_my_events['status'] == 1 && !empty($get_my_events['result'])) {
      $my_events = $get_my_events['result'];
    }
    $view_data = array();
    $view_data['menu_active'] = 'events';
    $view_data['event_date'] = $event_date;
    $view_data['event_message'] = $event_message;
    $view_data['my_events'] = $my_events;
    $this->load_view('event_create', $view_data);
    return;
  }

  public function update($event_id = NULL) {
    $update_event_status = NULL;
    $event_date = NULL;
    $event_message = NULL;
    if (!empty($event_id) && is_numeric($event_id)) {
      $get_event_by_id = $this->event_model->get_event_by_id($event_id);
      if ($get_event_by_id['status'] == 1 && !empty($get_event_by_id['result'])) {
        $event_data = $get_event_by_id['result'];
        $event_date = $event_data['event_date'];
        $event_message = $event_data['event_message'];
        if ($event_data['event_user_id'] == $this->current_user['user_id']) {
          if ($this->is_post()) {
            $update_event_status = 3;
            $event_date = $this->sanitize($this->input->post('event_date', TRUE));
            $event_message = $this->sanitize($this->input->post('event_message', TRUE));
            if (!empty($event_date) && !empty($event_message)) {
              $update_event_status = 2;
              if (strtotime($event_date)) {
                $update_event_status = 3;
                if (strlen($event_message) > 24 && strlen($event_message) < 251) {
                  $update_event_by_id = $this->event_model->update_event_by_id($event_id, $event_date, $event_message);
                  if ($update_event_by_id['status'] == 1 && !empty($update_event_by_id['result'])) {
                    $update_event_status = 1;
                    $this->session->set_flashdata('update_event_status', $update_event_status);
                    redirect('event/update/' . $event_id);
                    return;
                  }
                }
              }
            }
            $this->session->set_flashdata('update_event_status', $update_event_status);
            redirect('event/update/' . $event_id);
            return;
          }
          $my_events = NULL;
          $get_my_events = $this->event_model->get_my_events($this->current_user['user_id'], 10);
          if ($get_my_events['status'] == 1 && !empty($get_my_events['result'])) {
            $my_events = $get_my_events['result'];
          }
          $view_data = array();
          $view_data['menu_active'] = 'events';
          $view_data['event_id'] = $event_id;
          $view_data['event_date'] = $event_date;
          $view_data['event_message'] = $event_message;
          $view_data['my_events'] = $my_events;
          $this->load_view('event_update', $view_data);
          return;
        }
      }
    }

    redirect('tweets');
  }

  public function delete($event_id = NULL) {
    $delete_event_status = 2;
    if (!empty($event_id) && is_numeric($event_id)) {
      $get_event_by_id = $this->event_model->get_event_by_id($event_id);
      if ($get_event_by_id['status'] == 1 && !empty($get_event_by_id['result'])) {
        $event_data = $get_event_by_id['result'];
        if ($event_data['event_user_id'] == $this->current_user['user_id']) {
          $delete_event_by_id = $this->event_model->delete_event_by_id($event_id);
          if ($delete_event_by_id['status'] == 1 && !empty($delete_event_by_id['result'])) {
            $delete_event_status = 1;
          }
        }
      }
    }
    $this->session->set_flashdata('delete_event_status', $delete_event_status);
    redirect('event/myevents');
    return;
  }

}

?>
